package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.Scroller;

import java.util.ArrayList;
import java.util.List;

public class Practice11PieChartView extends View {

    //当前选择项的画笔
    private Paint selectPaint;

    //正常位置的画笔
    private Paint normalPaint;

    //选择区域
    private int selectArea;

    //一共多少个可见,必须为奇数
    private int visibleCount = 13;

    //数据总数
    private List<String> dataList;

    //选中区域线的高度
    private float selectAreaHeight = 180f;
    //选中区域线的宽度
    private float selectLineWidth = 7f;
    //未选中区域线的宽度
    private float normalLineWidth = 5f;

    //顶部字体的高度
    private float textHeight = 50f;
    //字体据View的距离
    private float textMargin = 10f;

    //view的高
    private int height;
    //view的宽
    private int width;

    //两侧线可见个数
    private int bothCount;
    //线的Y轴开始坐标
    private float itemHeightStart;

    //选中区域X轴开始坐标
    private int selectAreaStartX;
    //选中区域X轴结束坐标
    private int selectAreaEndX;

    //可见View的每个宽度
    private int itemWidth;
    //可见View的高度(占选中线的比例)
    private float itemLingHeight;

    //默认选中数据的item
    private int selectIndex = 0;

    //文字画笔
    private Paint textPaint;
    private Scroller mScroller;
    private float mXDown;
    private float mLastMoveX;
    private float mCurrentMoveX;
    private float leftBorder;
    private float rightBorder;

    public Practice11PieChartView(Context context) {
        this(context, null);
    }

    public Practice11PieChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Practice11PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 获取宽的测量模式
        int wSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        // 获取控件提供的 view 宽的最大值
        int wSpecSize = MeasureSpec.getSize(widthMeasureSpec);

        int hSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int hSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (wSpecMode == MeasureSpec.AT_MOST && hSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(300, 300);
        } else if (wSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(300, hSpecSize);
        } else if (hSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(wSpecSize, 300);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initParams();

        //左边界
        leftBorder = getLeft();
        //右边界
        rightBorder = getRight();

        //自定义横向wheel

        //左侧与右侧数量
        int tempCountR = bothCount, tempCountL = bothCount;

        //绘制左侧数据
        for (int i = dataList.size() - 1; i >= 0; i--) {
            if (i < selectIndex) {
                //左侧
                if (tempCountL < 1) {
                    break;
                } else {
                    canvas.drawLine(((itemWidth * tempCountL) - (itemWidth / 2)), itemHeightStart, (itemWidth * tempCountL) - (itemWidth / 2), itemHeightStart + itemLingHeight * tempCountL, normalPaint);
                    tempCountL--;
                }
            }
        }

        //绘制右侧数据
        for (int i = 0; i < dataList.size(); i++) {
            if (i > selectIndex) {
                //右侧
                if (tempCountR > bothCount * 2) {
                    break;
                } else {
                    canvas.drawLine(((itemWidth * (tempCountR + 2)) - (itemWidth / 2)), itemHeightStart, (itemWidth * (tempCountR + 2) - (itemWidth / 2)), itemHeightStart + itemLingHeight * (bothCount * 2 - tempCountR), normalPaint);
                    tempCountR++;
                }
            }
        }

        //绘制选中区域
        canvas.drawLine((itemWidth * selectArea) - (itemWidth / 2), itemHeightStart, (itemWidth * selectArea) - (itemWidth / 2), selectAreaHeight, selectPaint);
        //绘制文字
        canvas.drawText(dataList.get(selectIndex), (itemWidth * selectArea)- itemWidth , 25,textPaint);
    }


    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    public int getVisibleCount() {
        return visibleCount;
    }

    public void setVisibleCount(int visibleCount) {
        if (visibleCount > 1) {
            if (visibleCount % 2 == 0) {
                visibleCount -= 1;
            }
        } else {
            return;
        }
        this.visibleCount = visibleCount;
    }

    public List<String> getDataList() {
        return dataList;
    }

    public void setDataList(List<String> dataList) {
        this.dataList = dataList;
    }

    private void initPaint() {
        selectPaint = new Paint();
        selectPaint.setAntiAlias(true);
        selectPaint.setStyle(Paint.Style.FILL);
        selectPaint.setStrokeCap(Paint.Cap.ROUND);
        selectPaint.setStrokeWidth(selectLineWidth);
        selectPaint.setColor(Color.parseColor("#AAAAAA"));

        normalPaint = new Paint();
        normalPaint.setAntiAlias(true);
        normalPaint.setStyle(Paint.Style.FILL);
        normalPaint.setStrokeCap(Paint.Cap.ROUND);
        normalPaint.setStrokeWidth(normalLineWidth);
        normalPaint.setColor(Color.parseColor("#D7D7D7"));

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(28);


        dataList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            dataList.add("i" + i);
        }
        selectIndex = (dataList.size() - 1) / 2;
    }

    private void initParams() {
        width = getWidth();
        height = getHeight();

        //创建滑动实例
        mScroller = new Scroller(getContext());

        //求出每个view的宽度
        itemWidth = width / visibleCount;
        //线的高度,占选中直线的比例
        itemLingHeight = selectAreaHeight / visibleCount;
        //每条线Y轴开始坐标
        itemHeightStart = textHeight + textMargin;

        //绘制选中区域两侧可见个数
        bothCount = visibleCount / 2;
        //选中区域位置
        selectArea = bothCount + 1;

        //获取选中区域的X轴开始坐标
        selectAreaStartX = itemWidth * selectArea;
        //结束坐标
        selectAreaEndX = itemWidth * selectArea + itemWidth;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                float selectX = event.getX();
//                float selectY = event.getY();
//                inArea(selectX, selectY);
                //记录初始触摸屏幕下的坐标
                mXDown = event.getRawX();
                mLastMoveX = mXDown;
                break;
            case MotionEvent.ACTION_MOVE:
//                mCurrentMoveX = event.getRawX();
//                //本次的滑动距离
//                int scrolledX = (int) (mLastMoveX - mCurrentMoveX);
//                //如果右滑时 内容左边界超过初始化时候的左边界 就还是初始化时候的状态
//                Log.e("aaaaa",scrolledX+"---"+getScrollX()+"---"+leftBorder+"----"+rightBorder);
//                Log.e("aaaaa",scrolledX+"---"+getWidth()+"---"+getScrollX()+"----"+rightBorder);
//                if(getScrollX() + scrolledX < leftBorder){
//                    scrollTo((int) leftBorder,0);
//                }else if(getScrollX() + getWidth() + scrolledX > rightBorder){
//                    scrollTo((int) (rightBorder - getWidth()),0);
//                }else{
//                    //在左右边界中 自由滑动
//                    scrollBy(scrolledX,0);
//                }
//                mLastMoveX = mCurrentMoveX;
                scrollTo((int) event.getRawX(),0);

                break;
            case MotionEvent.ACTION_UP:
                inArea(event.getX(), event.getY());
                break;
        }
        return true;
    }

    /**
     * 计算是否在滑动区域内
     *
     * @param x
     * @param y
     */
    private void inArea(float x, float y) {
        //无效
        if (y < itemHeightStart || y > selectAreaHeight) {
            return;
        }
        //计算位于哪个线区域内
        int index = (int) x / itemWidth;
        int tempSelectIndex = selectIndex + index - selectArea + 1;
        if (tempSelectIndex < 0) {
            selectIndex = 0;
        } else if (tempSelectIndex > dataList.size() - 1) {
            selectIndex = dataList.size() - 1;
        } else {
            selectIndex = tempSelectIndex;
        }
//        invalidate();
    }
}
